﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bids.Core.DomainObjects;
using Bids.Core.Common.Data;
using Bids.Core.Common.Security;
using System.Linq.Expressions;
using Bids.Data;
using Bids.Core.Common.Pager;
using Bids.Core.DomainObjects;

namespace Bids.Web.Controllers.Admin
{
    public class UserController : BaseController<User>
    {

        
        public UserController(IRepository<User> iRpoUser)
            : base(iRpoUser)
        {
           
        }

        public override ActionResult Index(int page = 1)
        {
            OrderString();
            ConditionString();
            

            int listRows = 10;
            string t = Request.QueryString["t"];
            //处理搜索
            string Search_Field = Request["Search_Field"];
            string Search_Compare = Request["Search_Compare"];
            string Search_Key = Request["Search_Key"];
            string Search_Page = Request["listRows"];
            if (Search_Page != null)
            {
                listRows = int.Parse(Search_Page);
            }

            string Condition = conditionString;
            if (Search_Field != null && Search_Key != null)
            {

                switch (Search_Compare)
                {
                    case "Like":
                        Condition = Condition + " and " + Search_Field + ".Contains(\"" + Search_Key + "\")";
                        
                        break;
                }

            }
           
            if (!string.IsNullOrEmpty(t))
            {
                if(t == "cityagent")
                    Condition += " and !string.IsNullOrEmpty(AgentCity)";
            }
            IQueryable<User> list = _iRpoT.Where(Condition, orderString);

            //TempData["EModel"] = list;//导出的数据

            PagedList<User> li = list.ToPagedList(page, listRows);

            SetReturnUrl();

            return View(li);
        }

        /// <summary>
        /// 新增操作
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public override ActionResult Create(User model)
        {
            try
            {
                model.Login = new Login 
                {
                    UserName = model.UserName,
                    Password = Request.Form["Password"].ToMD5()
                };
                _iRpoT.Add(model);
                SetMessage("新增成功", true);
                return GetReturnUrl();
            }
            catch (Exception e)
            {
                SetMessage(e.Message.ToString().Replace("\r\n", "\\r\\n"), false);
                return RedirectToAction("Index");
            }
        }
        /// <summary>
        /// 修改操作
        /// </summary>
        /// <param name="id"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public override ActionResult Edit(int id,User moel)
        {
            try
            {
                var m = _iRpoT.Find(id);
                UpdateModel(m);
                string password = Request.Form["NewPassword"].Trim();
                if (!string.IsNullOrEmpty(password))
                {
                    m.Login.Password = password.ToMD5();
                }
                _iRpoT.Update(m);
                SetMessage("修改成功", true);
                return GetReturnUrl();
            }
            catch (Exception e)
            {
                SetMessage(e.Message.ToString().Replace("\r\n", "\\r\\n"), false);
                return RedirectToAction("Index");
            }
        }
        public string checkname(string LoginName)
        {
            string result = "";
            if (_iRpoT.Find(t=>t.UserName == LoginName)!=null)
            {
                result = "登录名已存在，请换一个登录名";
            }
            else
            {
                result = "true";
            }

            return result;
        }

    }
}
